package com.dreamchaser.familydevice.config.device.infunction;

import com.dreamchaser.familydevice.annotation.Function;
import com.dreamchaser.familydevice.config.SpringContextHolder;
import com.dreamchaser.familydevice.constant.DeviceCache;
import com.dreamchaser.familydevice.util.device.ByteHexUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.time.LocalTime;
import java.util.concurrent.TimeUnit;

/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 *
 *
 * @author: SJie
 * @Date: 2020/8/22 16:50
 * @description:
 *
 */
@Function(value = "time", descript = "校验时间区间",format = "time(15)/time(15:30)/time(15:30-19)/time(15:30-19,20-23)/")
@Slf4j
public class Time implements IFunction {
    @Override
    public boolean done(int deviceCode, String value, int data) {
        String[] values = value.split(",");
        boolean result = false;
        for (String s : values) {
            result = result || validRange(s);
            if (result) {
                return result;
            }
        }
        return false;

    }


    private boolean validRange(String range){
        String[] values = range.split("-");
        int hour = LocalTime.now().getHour();
        int minute = LocalTime.now().getMinute();
        String[] times = values[0].split(":");
        int _hour = Integer.valueOf(times[0]);

        if (values.length == 1) {

            if (_hour != hour) {
                return false;
            }else if (_hour == hour && times.length>1) {
                int _minute = Integer.valueOf(times[1]);
                if (_minute != minute) {
                    return false;
                }
            }
            return true;
        }
        else if (values.length == 2) {
            if (hour<_hour ) {
                return false;
            } else if (_hour == hour && times.length > 1) {
                int _minute = Integer.valueOf(times[1]);
                if (minute<_minute) {
                    return false;
                }
            }
            String[] times2 = values[1].split(":");
            int _hour2 = Integer.valueOf(times2[0]);
            if (hour >_hour2) {
                return false;
            }else if (hour ==_hour2 && times2.length > 1) {
                int _minute = Integer.valueOf(times2[1]);
                if (minute>_minute) {
                    return false;
                }
            }
        }
        return true;

    }
}


